package com.jzo2o.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.*;
import com.jzo2o.market.constants.TabTypeConstants;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.mapper.ActivityMapper;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.model.dto.response.SeizeCouponInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.jzo2o.market.constants.RedisConstants.RedisKey.*;
import static com.jzo2o.market.enums.ActivityStatusEnum.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
@Validated
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    private static final int MILLION = 1000000;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ICouponService couponService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;


    @Override
    public void addActivity(ActivitySaveReqDTO activitySaveReqDTO) {
        if (ObjectUtils.isNotEmpty(activitySaveReqDTO.getType())
                &&ObjectUtils.equals(activitySaveReqDTO.getType(),2)
            && ObjectUtils.isEmpty(activitySaveReqDTO.getDiscountRate())) {
            throw new BadRequestException("请输入折扣率");
        }
        Long userId = UserContext.currentUserId();


        Activity activity = BeanUtils.toBean(activitySaveReqDTO,
                Activity.class);
        activity.setCreateBy(userId);
        activity.setStatus(ActivityStatusEnum.NO_DISTRIBUTE.getStatus());
        activity.setStockNum(activitySaveReqDTO.getTotalNum());
//新增优惠活动和更新优惠活动 共用一个接口，以有没有id来判断，添加时id为空，更新时id不为空
        if (ObjectUtils.isEmpty(activitySaveReqDTO.getId())) {
            super.save(activity);
        }
        else {
            super.updateById(activity);
        }
    }

    @Override
    public PageResult<ActivityInfoResDTO> pageActivity(ActivityQueryForPageReqDTO activitySaveReqDTO) {

        return PageHelperUtils.selectPage(activitySaveReqDTO,
                () -> baseMapper.queryActivityList(activitySaveReqDTO));
    }

    @Override
    public ActivityInfoResDTO getDetail(Long id) {
            return baseMapper.getDetail(id);
    }

    @Override
    public void revokeActivity(Long id) {
        LambdaUpdateWrapper<Activity> query = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Activity> query2 = query.eq(Activity::getId, id)
//                ne =>  not equal
                .ne(Activity::getStatus, ActivityStatusEnum.VOIDED.getStatus())
                .set(Activity::getStatus,
                        ActivityStatusEnum.VOIDED.getStatus());
        int update = baseMapper.update(null, query2);
        if (update>0) {
            couponService.updateStatus(id);
        }
    }
}
